Skip to content

feat: Adds dataAttributes support to ButtonDropdown items#4247

Open
giridharsaday wants to merge 1 commit intocloudscape-design:mainfrom
giridharsaday:TricoderImpl
Open

feat: Adds dataAttributes support to ButtonDropdown items#4247
giridharsaday wants to merge 1 commit intocloudscape-design:mainfrom
giridharsaday:TricoderImpl

Conversation

@giridharsaday
Copy link

@giridharsaday giridharsaday commented Feb 12, 2026

Description

This PR adds support for custom data attributes on ButtonDropdown items, enabling teams to add analytics tracking and custom metadata without DOM manipulation workarounds.

Key Changes:

  • Add dataAttributes property to Item interface (CheckboxItem inherits it)
  • Automatically prefix attribute names with 'data-'
  • Prevent 'testid' override to preserve existing behavior
  • Add comprehensive unit tests including checkbox items

Use Case:
Teams building analytics-instrumented UIs (like NGS Hub with Tricorder) need to add data-* attributes to track user interactions consistently across multiple instances of the same action.

API Design:

<ButtonDropdown
 items={[
   {
     id: 'edit',
     text: 'Edit',
     dataAttributes: {
       'analytics-action': 'edit-product',
       'item-key': 'product-123'
     }
   }
 ]}
/>
// Renders as: data-analytics-action="edit-product" data-item-key="product-123"

Related links:

  • SIM Ticket: AWSUI-61657
  • API Proposal: [AdJuAxOmsoQW]

How has this been tested?

  • ✅ Added comprehensive unit tests covering:
    • Custom data attributes rendering
    • Automatic "data-" prefix addition
    • "testid" key exclusion
    • Undefined value handling
    • Multiple items
    • Disabled items
    • Checkbox items
  • ✅ Manually tested in dev environment - attributes render correctly on <li> elements
  • ✅ Verified existing tests still pass
  • ✅ Tested keyboard navigation and accessibility - no regressions

Reviewers can test by:

  1. Run unit tests: npm test -- src/button-dropdown/__tests__/data-attributes.test.tsx
  2. Start dev server: npm start
  3. Inspect dropdown items in browser DevTools to verify data-* attributes
Review checklist

Correctness

  • ✅ Changes include appropriate documentation updates (JSDoc comments on interface).
  • ✅ Changes are backward-compatible - new optional property.
  • ✅ Changes do not include unsupported browser features - uses standard data-* attributes.
  • ✅ Changes were manually tested for accessibility - no impact on screen readers or keyboard navigation.

Security

  • ✅ No URL handling in this change.

Testing

  • ✅ Changes are covered with new unit tests (7 test cases).
  • ⚠️ Integration tests not added yet - can be added if requested.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

- Add dataAttributes property to Item interface (CheckboxItem inherits it)
- Automatically prefix attribute names with 'data-'
- Prevent 'testid' override to preserve existing behavior
- Add comprehensive unit tests including checkbox items

Resolves: AWSUI-61657
@giridharsaday giridharsaday requested a review from a team as a code owner February 12, 2026 15:46
@giridharsaday giridharsaday requested review from cansuaa and removed request for a team February 12, 2026 15:46
@giridharsaday giridharsaday changed the title Add dataAttributes support to ButtonDropdown items feat : add dataAttributes support to ButtonDropdown items Feb 12, 2026
@pan-kot pan-kot self-requested a review February 13, 2026 09:16
@pan-kot pan-kot changed the title feat : add dataAttributes support to ButtonDropdown items feat : Adds dataAttributes support to ButtonDropdown items Feb 13, 2026
@pan-kot pan-kot changed the title feat : Adds dataAttributes support to ButtonDropdown items feat: Adds dataAttributes support to ButtonDropdown items Feb 13, 2026
* }
* }
* ]}
* // Renders as: data-analytics-action="edit-product" data-item-key="product-123"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These docs are not parsed by our docs extractor automatically. Please add it to the items prop description instead.

Let's make them less comprehensive also - I think a single sentence or two should be enough (it is important to mention the prefixing with data-.)

* - Filters out undefined values
*/
const getDataAttributes = (dataAttributes?: Record<string, string>): Record<string, string> => {
if (!dataAttributes) return {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails with our linting rules. Please use the prettier/eslint configuration of the package. You can also run npm run lint locally to see if there are some remaining errors.

return Object.entries(dataAttributes).reduce((acc, [key, value]) => {
// Exclude 'testid' to prevent overriding existing data-testid behavior
if (key === 'testid' || key === 'data-testid') {
console.warn('ButtonDropdown: "testid" key is reserved and cannot be overridden via dataAttributes');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use warnOnce utility for that.

* - Excludes 'testid' to prevent overriding existing behavior
* - Filters out undefined values
*/
const getDataAttributes = (dataAttributes?: Record<string, string>): Record<string, string> => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would move this util to /src/internal/utils - it will make it then easy to reuse for future similar use cases.

The util can probably take a list of properties to exclude.

/>
);

const item = getByText('Edit').closest('li');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use test-utils to find button dropdown items.

expect(item).toHaveAttribute('data-item-key', 'product-123');
});

test('automatically prepends data- prefix', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is that different from the prev test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the same time, there seems to be no test to ensure that the "data-" prefix is not added (again) when the passed property already has it.

expect(item).not.toHaveAttribute('data-undefined');
});

test('works with multiple items', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "works with multiple items", "works with disabled items", etc - can be combined into a single tests with multiple different items.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants